/**
 * Created by FreezeSoul on 2017/7/22.
 */
'use strict';
define([
    "app/core/base",
    "app/core/util",
    "../../controls/common/commonMeta",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'jqueryTmpl',
    'css!app/plugins/project/projectInfo/resources/style.css'
], function (Chart, Util, Meta, $, _, JSON, log) {

    var property = {
        config: "property",
        option: [
            {
                group: '绑定',
                id: 'bingdingData',
                name: '绑定值数据源',
                enable: true,
                type: 'sortBinding',
                dataset: '',
                columns: [],
                tooltip: '顺序按展示要求'
            }
        ].concat($.extend(true, [], Meta.commonProperties)),
        MapOption: function (option) {
            Util.linkOption(this.option, option);
        }
    };

    var table = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._lastdata = null;
            this._container = null;
            this._data = [];

            this._template = '<div class="projectinformation_container "><div class="projectinformation_row"></div></div>';
            this._templateI = '<div class="projectinformation_row_item">' +
                '<div class="projectinformation_title"><div class="projectinformation_auxiliary">&nbsp;</div><p id="projectinformation_colId">${key}</p></div>' +
                '<div class="projectinformation_value"><p id="projectinformation_colId">${value}</p><img src="/app/plugins/project/projectInfo/images/jiantou.png"  alt="jaintou" /></div>' +
                '</div>';
            this._header = [];
            this._option = {
                Binding: [],
                Events: [],
                Extend: $.extend(true, {}, property),
                Option: {
                    uuid: '',
                    class: '',
                    display: true,
                    data: [{key: "项目经理:", value: "南强强  15712819433  司龄3年"},
                        {key: "客户:", value: "名称   直营公司所在地：   湖南长沙"},
                        {key: "场区:", value: " 占地面积45平方米"},
                        {key: "道路:", value: "长度45KM   最大坡度45度"},
                        {key: "场内线路(35)KV:", value: " 电压等级（KV） 长度（KV）线塔数（基） 线路数"},
                        {key: "升压站(扩建、新建、容量:", value: "电压等级（KV） 长度（KV）线塔数（基）"},
                        {key: "基础(分类型数量:", value: "新建/扩建 容量(万千瓦)"},
                        {key: "道路:", value: "类型1 数量 类型2 数量"},
                        {key: "塔筒(分类型数量:", value: "类型1 数量 类型2 数量"},
                        {key: "吊车数量及型号:", value: "数量型号"}]
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },

        _render: function (option) {
            $(this._container).empty();
            var data = $.extend(true, [], option.data);
            var $row = $.tmpl(this._templateI, data);
            var $container = $(this._template);
            $row.appendTo($container.find(".projectinformation_row"));
            $container.appendTo(this._container);
            return $container;
        },
        example: function () {
            try {
                var exampleOption = {
                    uuid: '',
                    class: '',
                    display: true,
                    data: []
                };
                this._element = this._render(exampleOption);
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                Util.deepMapOption(property, this._option.Extend, this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },

        _bindData: function (data) {
            var bindProperty = _.findWhere(this._option.Extend.option, {id: 'bingdingData'});
            if (!bindProperty || !bindProperty.enable) return;

            var dataSetCode = bindProperty.dataset;
            var columns = bindProperty.columns;

            var table = data[dataSetCode];
            if (table && table.length === 1) {
                this._option.Option.data = [
                    {key: "项目经理:", value: table[0][columns[0].ColumnName]},
                    {key: "客户:", value: table[0][columns[1].ColumnName]},
                    {key: "场区:", value: table[0][columns[2].ColumnName]},
                    {key: "道路:", value: table[0][columns[3].ColumnName]},
                    {key: "场内线路(35)KV:", value: table[0][columns[4].ColumnName]},
                    {key: "升压站(扩建、新建、容量):", value: table[0][columns[5].ColumnName]},
                    {key: "基础(分类型数量):", value: table[0][columns[6].ColumnName]},
                    {key: "道路:", value: table[0][columns[7].ColumnName]},
                    {key: "塔筒(分类型数量):", value: table[0][columns[8].ColumnName]},
                    {key: "吊车数量及型号:", value: table[0][columns[9].ColumnName]}
                ];
            }else{
                this._option.Option.data = [
                    {key: "项目经理:", value: ""},
                    {key: "客户:", value: ""},
                    {key: "场区:", value: ""},
                    {key: "道路:", value: ""},
                    {key: "场内线路(35)KV:", value: ""},
                    {key: "升压站(扩建、新建、容量:", value: ""},
                    {key: "基础(分类型数量:", value: ""},
                    {key: "道路:", value: ""},
                    {key: "塔筒(分类型数量:", value: ""},
                    {key: "吊车数量及型号:", value: ""}
                ];
            }
        },
        setData: function (data) {
            try {
                this._readProperty();
                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    this._bindData(data);
                    this._readProperty();
                }
                this._element = this._render(this._option.Option);

            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                $(this._element).remove();

            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });
    return {
        Chart: table,
        Property: property
    };
});